package com.luotianacg.mode.observerPattern.realizeBySelf;

import java.util.ArrayList;

/**
 * @Author: luotian
 * @Date: 2023/4/9 23:14
 */
public class WeatherData implements Subject{
    private ArrayList observers;
    private float temperature;
    private float humidity;
    private float pressure;
    public WeatherData()
    {
        observers=new ArrayList();
    }

    @Override
    public void registerObservers(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        int i=observers.indexOf(o);
        if(i>=0)
        {
            observers.remove(o);
        }
    }

    @Override
    public void notifyObservers() {
        for(int i=0;i<observers.size();i++)
        {
            Observer o= (Observer) observers.get(i);
            o.update(temperature,humidity,pressure);
        }
    }
    public void measurementsChanged(){
        notifyObservers();
    }
    public void setMeasureMents(float temp,float humidity,float pressure)
    {
        this.temperature=temp;
        this.humidity=humidity;
        this.pressure=pressure;
        measurementsChanged();
    }
}
